//
//  LotteryManager.swift
//  MakeMoney
//
//  Created by Fan ZhiQiang on 2024/12/18.
//

import Foundation

let lotteryYears = [2025, 2024, 2023, 2022, 2021, 2020, 2019, 2018, 2017, 2016, 2015, 2014, 2013, 2012, 2011, 2010, 2009, 2008, 2007]

func getYearIdRange(with year: Int) -> Range<Int> {
    var yearLeft = year % 1000
    var yearRight = yearLeft + 1
    yearLeft = yearLeft * 1000 + 1
    yearRight = yearRight * 1000 + 1
    return yearLeft..<yearRight
}

func getLotterysWith(range: Range<Int>) -> [Lottery] {
    total.filter { lotter in
        return range.contains(lotter.yearId)
    }
}

func getLotterysWith(chooseNum: Int) -> [Lottery] {
    total.filter { lotter in
        return (lotter.front1 == chooseNum) || (lotter.front2 == chooseNum) || (lotter.front3 == chooseNum) || (lotter.front4 == chooseNum) || (lotter.front5 == chooseNum)
    }
}

func getLotterysWithCondition(startDate: Date, endDate: Date, front1: Int, front2: Int, front3: Int, front4: Int, front5: Int, hind1: Int, hind2: Int, ball: Int) -> [Lottery] {
    var lotterys = [Lottery]()
    lotterys = total.filter { lottery in
        lottery.date >= startDate && lottery.date <= endDate
    }
    
    if front1 != 0 {
        lotterys = lotterys.filter { lottery in
            lottery.front1 == front1
        }
    }
    
    if front2 != 0 {
        lotterys = lotterys.filter { lottery in
            lottery.front2 == front2
        }
    }
    
    if front3 != 0 {
        lotterys = lotterys.filter { lottery in
            lottery.front3 == front3
        }
    }
    
    if front4 != 0 {
        lotterys = lotterys.filter { lottery in
            lottery.front4 == front4
        }
    }
    
    if front5 != 0 {
        lotterys = lotterys.filter { lottery in
            lottery.front5 == front5
        }
    }
    
    if hind1 != 0 {
        lotterys = lotterys.filter { lottery in
            lottery.hind1 == hind1
        }
    }
    
    if hind2 != 0 {
        lotterys = lotterys.filter { lottery in
            lottery.hind2 == hind2
        }
    }
    
    if ball != 0 {
        lotterys = lotterys.filter { lottery in
            lottery.lotteryBall == ball
        }
    }
    
    return lotterys
}

func getStatisticsWith(_ lotterys: [Lottery]) -> LotteryStatistics {
    let statistic = LotteryStatistics()
    for lottery in lotterys {
        statisticFrontCount(lottery.front1, statistic: statistic)
        statisticFrontCount(lottery.front2, statistic: statistic)
        statisticFrontCount(lottery.front3, statistic: statistic)
        statisticFrontCount(lottery.front4, statistic: statistic)
        statisticFrontCount(lottery.front5, statistic: statistic)
        statisticHindCount(lottery.hind1, statistic: statistic)
        statisticHindCount(lottery.hind2, statistic: statistic)
        statisticBallCount(lottery.lotteryBall, statistic: statistic)
    }
    return statistic
}

func statisticFrontCount(_ number: Int, statistic: LotteryStatistics) {
    switch number {
    case 1:
        statistic.num1Count += 1
        statistic.frontCountDictionary[number] = statistic.num1Count
    case 2:
        statistic.num2Count += 1
        statistic.frontCountDictionary[number] = statistic.num2Count
    case 3:
        statistic.num3Count += 1
        statistic.frontCountDictionary[number] = statistic.num3Count
    case 4:
        statistic.num4Count += 1
        statistic.frontCountDictionary[number] = statistic.num4Count
    case 5:
        statistic.num5Count += 1
        statistic.frontCountDictionary[number] = statistic.num5Count
    case 6:
        statistic.num6Count += 1
        statistic.frontCountDictionary[number] = statistic.num6Count
    case 7:
        statistic.num7Count += 1
        statistic.frontCountDictionary[number] = statistic.num7Count
    case 8:
        statistic.num8Count += 1
        statistic.frontCountDictionary[number] = statistic.num8Count
    case 9:
        statistic.num9Count += 1
        statistic.frontCountDictionary[number] = statistic.num9Count
    case 10:
        statistic.num10Count += 1
        statistic.frontCountDictionary[number] = statistic.num10Count
    case 11:
        statistic.num11Count += 1
        statistic.frontCountDictionary[number] = statistic.num11Count
    case 12:
        statistic.num12Count += 1
        statistic.frontCountDictionary[number] = statistic.num12Count
    case 13:
        statistic.num13Count += 1
        statistic.frontCountDictionary[number] = statistic.num13Count
    case 14:
        statistic.num14Count += 1
        statistic.frontCountDictionary[number] = statistic.num14Count
    case 15:
        statistic.num15Count += 1
        statistic.frontCountDictionary[number] = statistic.num15Count
    case 16:
        statistic.num16Count += 1
        statistic.frontCountDictionary[number] = statistic.num16Count
    case 17:
        statistic.num17Count += 1
        statistic.frontCountDictionary[number] = statistic.num17Count
    case 18:
        statistic.num18Count += 1
        statistic.frontCountDictionary[number] = statistic.num18Count
    case 19:
        statistic.num19Count += 1
        statistic.frontCountDictionary[number] = statistic.num19Count
    case 20:
        statistic.num20Count += 1
        statistic.frontCountDictionary[number] = statistic.num20Count
    case 21:
        statistic.num21Count += 1
        statistic.frontCountDictionary[number] = statistic.num21Count
    case 22:
        statistic.num22Count += 1
        statistic.frontCountDictionary[number] = statistic.num22Count
    case 23:
        statistic.num23Count += 1
        statistic.frontCountDictionary[number] = statistic.num23Count
    case 24:
        statistic.num24Count += 1
        statistic.frontCountDictionary[number] = statistic.num24Count
    case 25:
        statistic.num25Count += 1
        statistic.frontCountDictionary[number] = statistic.num25Count
    case 26:
        statistic.num26Count += 1
        statistic.frontCountDictionary[number] = statistic.num26Count
    case 27:
        statistic.num27Count += 1
        statistic.frontCountDictionary[number] = statistic.num27Count
    case 28:
        statistic.num28Count += 1
        statistic.frontCountDictionary[number] = statistic.num28Count
    case 29:
        statistic.num29Count += 1
        statistic.frontCountDictionary[number] = statistic.num29Count
    case 30:
        statistic.num30Count += 1
        statistic.frontCountDictionary[number] = statistic.num30Count
    case 31:
        statistic.num31Count += 1
        statistic.frontCountDictionary[number] = statistic.num31Count
    case 32:
        statistic.num32Count += 1
        statistic.frontCountDictionary[number] = statistic.num32Count
    case 33:
        statistic.num33Count += 1
        statistic.frontCountDictionary[number] = statistic.num33Count
    case 34:
        statistic.num34Count += 1
        statistic.frontCountDictionary[number] = statistic.num34Count
    case 35:
        statistic.num35Count += 1
        statistic.frontCountDictionary[number] = statistic.num35Count
    default:
        print("不操作")
    }
}

func statisticHindCount(_ number: Int, statistic: LotteryStatistics) {
    switch number {
    case 1:
        statistic.no1Count += 1
        statistic.hindCountDictionary[number] = statistic.no1Count
    case 2:
        statistic.no2Count += 1
        statistic.hindCountDictionary[number] = statistic.no2Count
    case 3:
        statistic.no3Count += 1
        statistic.hindCountDictionary[number] = statistic.no3Count
    case 4:
        statistic.no4Count += 1
        statistic.hindCountDictionary[number] = statistic.no4Count
    case 5:
        statistic.no5Count += 1
        statistic.hindCountDictionary[number] = statistic.no5Count
    case 6:
        statistic.no6Count += 1
        statistic.hindCountDictionary[number] = statistic.no6Count
    case 7:
        statistic.no7Count += 1
        statistic.hindCountDictionary[number] = statistic.no7Count
    case 8:
        statistic.no8Count += 1
        statistic.hindCountDictionary[number] = statistic.no8Count
    case 9:
        statistic.no9Count += 1
        statistic.hindCountDictionary[number] = statistic.no9Count
    case 10:
        statistic.no10Count += 1
        statistic.hindCountDictionary[number] = statistic.no10Count
    case 11:
        statistic.no11Count += 1
        statistic.hindCountDictionary[number] = statistic.no11Count
    case 12:
        statistic.no12Count += 1
        statistic.hindCountDictionary[number] = statistic.no12Count
    default:
        print("不操作")
    }
}

func statisticBallCount(_ number: Int, statistic: LotteryStatistics) {
    switch number {
    case 1:
        statistic.useBall1Count += 1
        statistic.ballCountDictionary[number] = statistic.useBall1Count
    case 2:
        statistic.useBall2Count += 1
        statistic.ballCountDictionary[number] = statistic.useBall2Count
    case 3:
        statistic.useBall3Count += 1
        statistic.ballCountDictionary[number] = statistic.useBall3Count
    default:
        print("不操作")
    }
}

func getDateIntervalWith(_ lotterys: [Lottery]) -> DigitStatistic {
    let digit = DigitStatistic()
    if lotterys.isEmpty {
        return digit
    }
    digit.dates.append(lotterys.first?.date ?? Date())
    var misplacements = lotterys
    misplacements.removeFirst()
    var index = 0
    for misplacement in misplacements {
        digit.dates.append(misplacement.date)
        let lottery = lotterys[index]
        let dateComponent = Calendar.current.dateComponents([.day], from: misplacement.date, to: lottery.date)
        digit.days.append(String(dateComponent.day ?? 0))
        index += 1
    }
    
    return digit
}
